package idiet.view;

import idiet.Activator;
import idiet.bean.Customer;
import idiet.bean.CustomerPal;
import idiet.bean.Nation;
import idiet.bean.NurseTarget;
import idiet.service.CustomerPalService;
import idiet.service.CustomerService;
import idiet.service.NationService;
import idiet.service.NurseTargetService;
import idiet.util.Validate;

import java.awt.Color;
import java.math.BigDecimal;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.ResourceBundle;

import org.apache.commons.lang.StringUtils;
import org.eclipse.jface.action.IStatusLineManager;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DateTime;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.part.ViewPart;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;

public class ViewPartCustomerAdd extends ViewPart {
	private static final ResourceBundle BUNDLE = ResourceBundle.getBundle("idiet.view.messages"); //$NON-NLS-1$
	public static final String ID="iDiet.view.customer.add";
	private CustomerPalService palService=(CustomerPalService)Activator.getApplicationContext().getBean("customerPalService");
	private NurseTargetService nurseTargetService=(NurseTargetService)Activator.getApplicationContext().getBean("nurseTargetService");
	private NationService nationService=(NationService)Activator.getApplicationContext().getBean("nationService");
	private CustomerService customerService=(CustomerService)Activator.getApplicationContext().getBean("customerService");
	private Text textName;
	private Text textPhone;
	private Text textCellPhone;
	private Text textIM;
	private Text textEmail;
	private Text textAddress;
	private Text textHeight;
	private Text textWeight;
	private Text textNurseComment;
	private Text textAge;
	private Text textPicturePath;
	private Combo comboNation;
	private Combo comboPal;
	private Combo comboNurseTarget;
	private DateTime dateTimeForecastStart;
	private DateTime dateTimeForecastEnd;
	private Button btnRadioButtonMale;
	private Button btnRadioButtonFemale;
	private Canvas canvas;
	private Button btnCreateCustomer;
	private Image image;
	private Customer customer;
	private Text textBust;
	private Text textWaist;
	private Text textHip;
	 /*private int startX = -1;
	    private int startY = -1;
	    private int endX = 0;
	    private int endY = 0;
	    private int offsetX = 0;
	    private int offsetY = 0;
	    private int x = 0;
	    private int y = 0;
	    private boolean moveBool = true;
	    private boolean custBool = false;
	    Double a = null;
	    Double b = null;*/
	public ViewPartCustomerAdd() {
		// TODO Auto-generated constructor stub
		
	}

	@Override
	public void createPartControl(Composite parent) {
		parent.setLayout(new GridLayout(2, false));
		
		Group group = new Group(parent, SWT.NONE);
		group.setLayout(new GridLayout(3, false));
		GridData gd_group = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		gd_group.heightHint = 208;
		group.setLayoutData(gd_group);
		
		Label lblNewLabel = new Label(group, SWT.NONE);
		lblNewLabel.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
		lblNewLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblNewLabel.setText(BUNDLE.getString("ViewPartCustomerAdd.lblNewLabel.text")); //$NON-NLS-1$
		
		textName = new Text(group, SWT.BORDER);
		textName.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				if(textName.getText().trim().equals("")){
					showErrorOnStatusline(BUNDLE.getString("View.Part.Customer.Add.name.is.empty"));
					btnCreateCustomer.setEnabled(false);
				}else{
					showErrorOnStatusline(null);
					btnCreateCustomer.setEnabled(true);
				}
			}
		});
		textName.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
		
		Label lblNewLabel_9 = new Label(group, SWT.NONE);
		lblNewLabel_9.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
		lblNewLabel_9.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblNewLabel_9.setText(BUNDLE.getString("ViewPartCustomerAdd.lblNewLabel_9.text"));
		
		textAge = new Text(group, SWT.BORDER);
		textAge.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				if(textAge.getText().trim().equals("")){
					showErrorOnStatusline(BUNDLE.getString("View.Part.Customer.Add.age.is.empty"));
					btnCreateCustomer.setEnabled(false);
				}else{
					if(!StringUtils.isNumeric(textAge.getText())){
						textAge.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
						showErrorOnStatusline(BUNDLE.getString("View.Part.Customer.Add.age.is.not.numeric"));
						btnCreateCustomer.setEnabled(false);
					}else{
						showErrorOnStatusline(null);
						textAge.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLACK));
						btnCreateCustomer.setEnabled(true);
					}
					
				}
			}
		});
		textAge.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
		
		Label lblNewLabel_10 = new Label(group, SWT.NONE);
		lblNewLabel_10.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
		lblNewLabel_10.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblNewLabel_10.setAlignment(SWT.RIGHT);
		lblNewLabel_10.setText(BUNDLE.getString("ViewPartCustomerAdd.lblNewLabel_10.text"));
		
		textHeight = new Text(group, SWT.BORDER);
		textHeight.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				if(textHeight.getText().trim().equals("")){
					showErrorOnStatusline(BUNDLE.getString("View.Part.Customer.Add.height.is.empty"));
					btnCreateCustomer.setEnabled(false);
				}else{
					if(!StringUtils.isNumeric(textHeight.getText())){
						showErrorOnStatusline(BUNDLE.getString("View.Part.Customer.Add.height.is.not.numeric"));
						btnCreateCustomer.setEnabled(false);
						textHeight.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
					}else{
						showErrorOnStatusline(null);
						btnCreateCustomer.setEnabled(true);
						textHeight.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLACK));
					}
				}
			}
		});
		textHeight.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		
		Label lblcm = new Label(group, SWT.NONE);
		lblcm.setText(BUNDLE.getString("ViewPartCustomerAdd.lblcm.text"));
		
		Label lblNewLabel_11 = new Label(group, SWT.NONE);
		lblNewLabel_11.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
		lblNewLabel_11.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblNewLabel_11.setText(BUNDLE.getString("ViewPartCustomerAdd.lblNewLabel_11.text"));
		
		textWeight = new Text(group, SWT.BORDER);
		textWeight.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				if(textWeight.getText().trim().equals("")){
					showErrorOnStatusline(BUNDLE.getString("View.Part.Customer.Add.weight.is.empty"));
					btnCreateCustomer.setEnabled(false);
				}else{
					if(!StringUtils.isNumeric(textWeight.getText())){
						showErrorOnStatusline(BUNDLE.getString("View.Part.Customer.Add.weight.is.not.numeric"));
						btnCreateCustomer.setEnabled(false);
						textWeight.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
					}else{
						showErrorOnStatusline(null);
						btnCreateCustomer.setEnabled(true);
						textWeight.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLACK));
					}
				}
			}
		});
		textWeight.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		
		Label lblkg = new Label(group, SWT.NONE);
		lblkg.setText(BUNDLE.getString("ViewPartCustomerAdd.lblkg.text"));
		
		Label lblNewLabel_1 = new Label(group, SWT.NONE);
		lblNewLabel_1.setForeground(SWTResourceManager.getColor(0, 0, 0));
		lblNewLabel_1.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblNewLabel_1.setText(BUNDLE.getString("ViewPartCustomerAdd.lblNewLabel_1.text")); //$NON-NLS-1$
		
		Composite composite = new Composite(group, SWT.NONE);
		composite.setLayout(new GridLayout(2, false));
		
		btnRadioButtonMale = new Button(composite, SWT.RADIO);
		btnRadioButtonMale.setSelection(true);
		btnRadioButtonMale.setText(BUNDLE.getString("ViewPartCustomerAdd.btnRadioButtonMale.text")); //$NON-NLS-1$
		
		btnRadioButtonFemale = new Button(composite, SWT.RADIO);
		btnRadioButtonFemale.setText(BUNDLE.getString("ViewPartCustomerAdd.btnRadioButtonFemale.text")); //$NON-NLS-1$
		new Label(group, SWT.NONE);
		
		Label lblNewLabel_2 = new Label(group, SWT.NONE);
		lblNewLabel_2.setForeground(SWTResourceManager.getColor(0, 0, 0));
		lblNewLabel_2.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblNewLabel_2.setText(BUNDLE.getString("ViewPartCustomerAdd.lblNewLabel_2.text")); //$NON-NLS-1$
		
		comboNation = new Combo(group, SWT.NONE);
		comboNation.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String selectionText=comboNation.getText();
				System.out.println(selectionText);
				System.out.println(comboNation.getData(selectionText));
			}
		});
		comboNation.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		List<Nation> nations=nationService.getNationsByNationName(null);
		for(Nation nation:nations){
			comboNation.add(nation.getName());
			comboNation.setData(nation.getName(),nation.getId());
		}
		//comboNation.setItems(nationService.getNationsByNationName(null).toArray(new String[0]));
		new Label(group, SWT.NONE);
		
		Label lblNewLabel_3 = new Label(group, SWT.NONE);
		lblNewLabel_3.setForeground(SWTResourceManager.getColor(SWT.COLOR_DARK_GREEN));
		lblNewLabel_3.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblNewLabel_3.setText(BUNDLE.getString("ViewPartCustomerAdd.lblNewLabel_3.text")); //$NON-NLS-1$
		
		textPhone = new Text(group, SWT.BORDER);
		textPhone.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				showErrorOnStatusline(null);
				btnCreateCustomer.setEnabled(true);
			}
		});
		textPhone.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
		
		Label lblNewLabel_4 = new Label(group, SWT.NONE);
		lblNewLabel_4.setForeground(SWTResourceManager.getColor(SWT.COLOR_DARK_GREEN));
		lblNewLabel_4.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblNewLabel_4.setText(BUNDLE.getString("ViewPartCustomerAdd.lblNewLabel_4.text")); //$NON-NLS-1$
		
		textCellPhone = new Text(group, SWT.BORDER);
		textCellPhone.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				showErrorOnStatusline(null);
				btnCreateCustomer.setEnabled(true);
			}
		});
		textCellPhone.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
		
		Label lblNewLabel_5 = new Label(group, SWT.NONE);
		lblNewLabel_5.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblNewLabel_5.setText(BUNDLE.getString("ViewPartCustomerAdd.lblNewLabel_5.text")); //$NON-NLS-1$
		
		textIM = new Text(group, SWT.BORDER);
		textIM.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
		
		Label lblNewLabel_6 = new Label(group, SWT.NONE);
		lblNewLabel_6.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblNewLabel_6.setText(BUNDLE.getString("ViewPartCustomerAdd.lblNewLabel_6.text")); //$NON-NLS-1$
		
		textEmail = new Text(group, SWT.BORDER);
		textEmail.addFocusListener(new FocusAdapter() {
			@Override
			public void focusLost(FocusEvent e) {
				if(!Validate.emailValidate(textEmail.getText())){
					textEmail.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
					showErrorOnStatusline(BUNDLE.getString("View.Part.Customer.Add.email.error.format"));
					btnCreateCustomer.setEnabled(false);
				}else{
					textEmail.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLACK));
					showErrorOnStatusline(null);
					btnCreateCustomer.setEnabled(true);
				}
				//textEmail.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLACK));
				System.out.println("ddddddddddddddd");
			}
		});
		textEmail.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
		
		Label lblNewLabel_7 = new Label(group, SWT.NONE);
		lblNewLabel_7.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblNewLabel_7.setText(BUNDLE.getString("ViewPartCustomerAdd.lblNewLabel_7.text")); //$NON-NLS-1$
		
		textAddress = new Text(group, SWT.BORDER);
		textAddress.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
		
		Label lblNewLabel_8 = new Label(group, SWT.NONE);
		lblNewLabel_8.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblNewLabel_8.setText(BUNDLE.getString("ViewPartCustomerAdd.lblNewLabel_8.text")); //$NON-NLS-1$
		
		textBust = new Text(group, SWT.BORDER);
		textBust.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				if(!StringUtils.isNumeric(textBust.getText())){
					showErrorOnStatusline(BUNDLE.getString("View.Part.Customer.Add.bust.is.not.numeric"));
					btnCreateCustomer.setEnabled(false);
					textBust.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
				}else{
					showErrorOnStatusline(null);
					btnCreateCustomer.setEnabled(true);
					textBust.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLACK));
				}
			}
		});
		textBust.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label label_1 = new Label(group, SWT.NONE);
		label_1.setText(BUNDLE.getString("ViewPartCustomerAdd.lblcm.text"));
		
		Label lblNewLabel_16 = new Label(group, SWT.NONE);
		lblNewLabel_16.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblNewLabel_16.setText(BUNDLE.getString("ViewPartCustomerAdd.lblNewLabel_16.text")); //$NON-NLS-1$
		
		textWaist = new Text(group, SWT.BORDER);
		textWaist.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		textWaist.addModifyListener(new ModifyListener(){

			@Override
			public void modifyText(ModifyEvent e) {
				// TODO Auto-generated method stub
				if(!StringUtils.isNumeric(textWaist.getText())){
					showErrorOnStatusline(BUNDLE.getString("View.Part.Customer.Add.waist.is.not.numeric"));
					btnCreateCustomer.setEnabled(false);
					textWaist.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
				}else{
					showErrorOnStatusline(null);
					btnCreateCustomer.setEnabled(true);
					textWaist.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLACK));
				}
			}
		});
		
		Label label_2 = new Label(group, SWT.NONE);
		label_2.setText(BUNDLE.getString("ViewPartCustomerAdd.lblcm.text"));
		
		Label lblNewLabel_18 = new Label(group, SWT.NONE);
		lblNewLabel_18.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblNewLabel_18.setText(BUNDLE.getString("ViewPartCustomerAdd.lblNewLabel_18.text_1")); //$NON-NLS-1$
		
		textHip = new Text(group, SWT.BORDER);
		textHip.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		textHip.addModifyListener(new ModifyListener(){

			@Override
			public void modifyText(ModifyEvent e) {
				// TODO Auto-generated method stub
				if(!StringUtils.isNumeric(textHip.getText())){
					showErrorOnStatusline(BUNDLE.getString("View.Part.Customer.Add.hip.is.not.numeric"));
					btnCreateCustomer.setEnabled(false);
					textHip.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
				}else{
					showErrorOnStatusline(null);
					btnCreateCustomer.setEnabled(true);
					textHip.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLACK));
				}
			}
			
		});
		
		Label label_3 = new Label(group, SWT.NONE);
		label_3.setText(BUNDLE.getString("ViewPartCustomerAdd.lblcm.text"));
		
		Label lblNewLabel_12 = new Label(group, SWT.NONE);
		lblNewLabel_12.setForeground(SWTResourceManager.getColor(0, 0, 0));
		lblNewLabel_12.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblNewLabel_12.setText(BUNDLE.getString("ViewPartCustomerAdd.lblNewLabel_12.text"));
		
		comboPal = new Combo(group, SWT.NONE);
		comboPal.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
		List<CustomerPal> pals=palService.getCustomerPalsByCustomerPalName(null);
		for(CustomerPal pal:pals){
			comboPal.add(pal.getName());
			comboPal.setData(pal.getName(), pal.getId());
		}
		new Label(group, SWT.NONE);
		
		Label lblNewLabel_13 = new Label(group, SWT.NONE);
		lblNewLabel_13.setForeground(SWTResourceManager.getColor(0, 0, 0));
		lblNewLabel_13.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblNewLabel_13.setText(BUNDLE.getString("ViewPartCustomerAdd.lblNewLabel_13.text"));
		
		comboNurseTarget = new Combo(group, SWT.NONE);
		comboNurseTarget.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		List<NurseTarget> targets=this.nurseTargetService.getNurseTargetsByNurseTargetName(null);
		for(NurseTarget target:targets){
			comboNurseTarget.add(target.getName());
			comboNurseTarget.setData(target.getName(), target.getId());
		}
		new Label(group, SWT.NONE);
		
		Label lblNewLabel_14 = new Label(group, SWT.NONE);
		lblNewLabel_14.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblNewLabel_14.setText(BUNDLE.getString("ViewPartCustomerAdd.lblNewLabel_14.text"));
		
		dateTimeForecastStart = new DateTime(group, SWT.BORDER | SWT.DROP_DOWN | SWT.LONG);
		dateTimeForecastStart.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		new Label(group, SWT.NONE);
		
		Label lblNewLabel_15 = new Label(group, SWT.NONE);
		lblNewLabel_15.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblNewLabel_15.setText(BUNDLE.getString("ViewPartCustomerAdd.lblNewLabel_15.text"));
		
		dateTimeForecastEnd = new DateTime(group, SWT.BORDER | SWT.DROP_DOWN | SWT.LONG);
		dateTimeForecastEnd.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		//dateTimeForecastEnd.
		new Label(group, SWT.NONE);
		Calendar calendar=GregorianCalendar.getInstance();
		calendar.add(Calendar.DAY_OF_MONTH, 30);
		dateTimeForecastEnd.setDate(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
		
		Label lblNewLabel_17 = new Label(group, SWT.NONE);
		lblNewLabel_17.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, false, false, 1, 1));
		lblNewLabel_17.setText(BUNDLE.getString("View.common.comment.text"));
		
		textNurseComment = new Text(group, SWT.BORDER | SWT.WRAP | SWT.V_SCROLL | SWT.MULTI);
		GridData gd_textNurseComment = new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1);
		gd_textNurseComment.heightHint = 48;
		textNurseComment.setLayoutData(gd_textNurseComment);
		
		Composite composite_1 = new Composite(parent, SWT.NONE);
		composite_1.setLayout(new GridLayout(3, false));
		GridData gd_composite_1 = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		gd_composite_1.widthHint = 233;
		composite_1.setLayoutData(gd_composite_1);
		
		Label label = new Label(composite_1, SWT.NONE);
		label.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label.setText(BUNDLE.getString("ViewPartCustomerAdd.label.text")); //$NON-NLS-1$
		
		textPicturePath = new Text(composite_1, SWT.BORDER);
		textPicturePath.setEditable(false);
		textPicturePath.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Button btnBrowerPicture = new Button(composite_1, SWT.NONE);
		btnBrowerPicture.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				FileDialog dialog=new FileDialog(getSite().getShell(),SWT.OPEN);
				dialog.setFilterPath(System.getProperty("user.home"));
		        String[] filterExt = { "*.jpg;*.jpeg","*.jpeg", "*.doc", ".rtf", "*.*" };
		        dialog.setFilterExtensions(filterExt);
		     // Set the text
		        dialog.setText("Select File");
		        // Set filter on .txt files
		        dialog.setFilterExtensions(filterExt);
		        // Put in a readable name for the filter
		        //dialog.setFilterNames(new String[] { "Textfiles(*.txt)" });
		        // Open Dialog and save result of selection
		        String selectionFile = dialog.open();
		        if(selectionFile!=null){
		        	textPicturePath.setText(selectionFile);
		        	image = new Image(getSite().getShell().getDisplay(),selectionFile);
		        	canvas.redraw();
		        }
		        System.out.println(selectionFile);
			}
		});
		btnBrowerPicture.setText(BUNDLE.getString("ViewPartCustomerAdd.btnBrowerPicture.text"));
		
		canvas = new Canvas(composite_1, SWT.BORDER);
		/*canvas.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				 // 获得终点位置坐标
                endX = e.x;
                endY = e.y;
                // 拖动的偏移量
                offsetX = endX - startX;
                offsetY = endY - startY;
                System.out.println("开始坐标为：(" + x + "," + y + ")");
            
                // 拖动后的图片的位置
                x = canvas.getBounds().x + offsetX;
                y = canvas.getBounds().y + offsetY;
                
                System.out.println("坐标为：(" + x + "," + y + ")");
                if (moveBool) {// 为True表示可以拖动图片
                    // 再次重新设置图片的位置
                    canvas.setBounds(x, y, canvas.getSize().x,canvas.getSize().y);
                    canvas.setRedraw(true);
                }
                if (custBool) {//为True表示可以对图片进行截图
                        
                }
			}
			@Override
			public void mouseDown(MouseEvent e) {
				// 获得初始位置的坐标
                if (startX == -1 && startY == -1) {
                    startX = e.x;
                    startY = e.y;
                }
			}
		});*/
		canvas.addPaintListener(new PaintListener() {
			public void paintControl(PaintEvent e) {
				if(image!=null){
					/*int width = (int)(image.getBounds().width * rate);  
                    int heigth = (int)(image.getBounds().height * rate);*/  
                    //e.gc.drawImage(image, 0, 0, canvas.getBounds().width, canvas.getBounds().height, 0, 0, image.getBounds().width, image.getBounds().height);
					System.out.println("canvas width "+canvas.getBounds().width+" , height"+canvas.getBounds().height);
					System.out.println("image width "+image.getBounds().width+" , height"+image.getBounds().height);
					int imageWidth=image.getBounds().width;
					int imageHeight=image.getBounds().height;
					int canvasWidth=canvas.getBounds().width;
					int canvasHeight=canvas.getBounds().height;
					int destWidth=imageWidth;
					int destHeight=imageHeight;
					int destX=0;
					int destY=0;
					double rate=(double)imageWidth/imageHeight;
					if(imageWidth>canvasWidth){
						destWidth=canvasWidth;
					}
					if(imageHeight>canvasHeight){
						destHeight=canvasHeight;
					}
					if(rate>1){
						destHeight=(int)(destHeight/rate);
					}else{
						destWidth=(int)(destWidth*rate);
					}
					final Image scaledImage = new Image(getSite().getShell().getDisplay(),image.getImageData().scaledTo(destWidth,destHeight));
					destX=(canvas.getBounds().width-destWidth)/2;
					destY=(canvas.getBounds().height-destHeight)/2;
                    e.gc.drawImage(scaledImage, destX, destY); 
                    //image=null;
					//e.gc.drawImage(image, 0, 0);
					/* a =  0.5*(canvas.getBounds().width-image.getBounds().width);
	                    b = 0.5*(canvas.getBounds().height - image.getBounds().height);
	                    Rectangle rectangle = new Rectangle(a.intValue(), b.intValue(), image.getBounds().width, image.getBounds().height);
	                    canvas.setBounds(rectangle);
	                    e.gc.drawImage(image,a.intValue(), b.intValue());*/
				}
			}
		});
		GridData gd_canvas = new GridData(SWT.FILL, SWT.FILL, true, true, 3, 2);
		gd_canvas.widthHint = 299;
		gd_canvas.heightHint = 143;
		canvas.setLayoutData(gd_canvas);
		
		btnCreateCustomer = new Button(composite_1, SWT.NONE);
		btnCreateCustomer.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if(textName.getText().trim().equals("")){
					showErrorOnStatusline(BUNDLE.getString("View.Part.Customer.Add.name.is.empty"));
					btnCreateCustomer.setEnabled(false);
					return;
				}
				if(textAge.getText().trim().equals("")){
					showErrorOnStatusline(BUNDLE.getString("View.Part.Customer.Add.age.is.empty"));
					btnCreateCustomer.setEnabled(false);
					return;
				}
				if(textHeight.getText().trim().equals("")){
					showErrorOnStatusline(BUNDLE.getString("View.Part.Customer.Add.height.is.empty"));
					btnCreateCustomer.setEnabled(false);
					return;
				}
				if(textWeight.getText().trim().equals("")){
					showErrorOnStatusline(BUNDLE.getString("View.Part.Customer.Add.weight.is.empty"));
					btnCreateCustomer.setEnabled(false);
					return;
				}
				customer=new Customer();
				customer.setName(textName.getText());
				customer.setAge(Integer.parseInt(textAge.getText()));
				customer.setHeight(new BigDecimal(textHeight.getText()));
				customer.setWeight(new BigDecimal(textWeight.getText()));
				if(btnRadioButtonFemale.getSelection()){
			  		customer.setGender("女");
				}else{
					customer.setGender("男");
				}
				if(comboNation.getText().length()!=0){
					Nation nation=new Nation();
					nation.setId(Integer.parseInt(comboNation.getData(comboNation.getText()).toString()));
					nation.setName(comboNation.getText());
					customer.setNation(nation);
				}else{
					customer.setNation(null);
				}
				if(textPhone.getText().trim().equals("")&&textCellPhone.getText().trim().equals("")){
					showErrorOnStatusline(BUNDLE.getString("View.Part.Customer.Add.phone.at.least"));
					btnCreateCustomer.setEnabled(false);
				}
				customer.setTelephone(textPhone.getText());
				customer.setCellphone(textCellPhone.getText());
				customer.setIm(textIM.getText());
				customer.setEmail(textEmail.getText());
				customer.setPostaladdress(textAddress.getText());
				customer.setWaist(new BigDecimal(textWaist.getText()));
				customer.setHips(new BigDecimal(textHip.getText()));
				customer.setBust(new BigDecimal(textBust.getText()));
				if(!comboPal.getText().trim().equals("")){
					CustomerPal pal=new CustomerPal();
					pal.setId(Integer.parseInt(comboPal.getData(comboPal.getText()).toString()));
					pal.setName(comboPal.getText());
					customer.setCustomerPal(pal);
				}else{
					customer.setCustomerPal(null);
				}
				Calendar startTime = GregorianCalendar.getInstance();
				startTime.set(dateTimeForecastStart.getYear(), dateTimeForecastStart.getMonth(), dateTimeForecastStart.getDay(), dateTimeForecastStart.getHours(), dateTimeForecastStart.getMinutes());
				customer.setForeStartDate(startTime.getTime());
				Calendar endTime=GregorianCalendar.getInstance();
				endTime.set(dateTimeForecastEnd.getYear(), dateTimeForecastEnd.getMonth(), dateTimeForecastEnd.getMonth(), dateTimeForecastEnd.getDay(), dateTimeForecastEnd.getMinutes());
				customer.setForeEndDate(endTime.getTime());
				customer.setBasicComment(textNurseComment.getText());
				customerService.createCustomer(customer);
				
				textName.setText("");
				textPhone.setText("");
				textCellPhone.setText("");
				textIM.setText("");
				textEmail.setText("");
				textAddress.setText("");
				textHeight.setText("");
				textWeight.setText("");
				textNurseComment.setText("");
				textAge.setText("");
				textWaist.setText("");
				textHip.setText("");
				textBust.setText("");
				textPicturePath.setText("");
				comboNation.setText("");
				comboPal.setText("");
				comboNurseTarget.setText("");
				//dateTimeForecastStart.set;
				//dateTimeForecastEnd;
				btnRadioButtonMale.setSelection(true);
				showErrorOnStatusline(null);
			}
		});
		GridData gd_btnCreateCustomer = new GridData(SWT.LEFT, SWT.TOP, false, false, 1, 1);
		gd_btnCreateCustomer.widthHint = 60;
		btnCreateCustomer.setLayoutData(gd_btnCreateCustomer);
		btnCreateCustomer.setText(BUNDLE.getString("ViewPartCustomerAdd.btnCreateCustomer.text"));
		new Label(composite_1, SWT.NONE);
		new Label(composite_1, SWT.NONE);
		// TODO Auto-generated method stub

	}

	@Override
	public void setFocus() {
		// TODO Auto-generated method stub
	}
	protected void showErrorOnStatusline(String error) {
		IStatusLineManager statusline = this.getViewSite().getActionBars().getStatusLineManager();
		if (error == null) {
			statusline.setErrorMessage(null, error);
		} else {
			statusline.setErrorMessage(null, error);
		} 
	}
}
